<template>
  <div class="my-goods-item">
    <div class="left">
      <div class="custom-control custom-checkbox">
        <input
          type="checkbox"
          class="custom-control-input"
          :id="gObj.id"
          v-model="gObj.goods_state"
        />
        <label class="custom-control-label" :for="gObj.id">
          <img :src="gObj.goods_img" alt="" />
        </label>
      </div>
    </div>
    <div class="right">
      <div class="top">{{ gObj.goods_name }}</div>
      <div class="bottom">
        <span class="price">¥ {{ gObj.goods_price }}</span>
        <!-- 数量控制 -->
        <div class="my-counter">
          <button
            type="button"
            class="btn btn-light"
            :disabled="gObj.goods_count === 1"
            @click="gObj.goods_count > 1 && gObj.goods_count--"
          >
            -
          </button>
          <input
            type="number"
            class="form-control inp"
            v-model.number="gObj.goods_count"
          />
          <button
            type="button"
            class="btn btn-light"
            @click="gObj.goods_count++"
          >
            +
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    gObj: {
      type: Object,
      default: () => {}
    }
  },
  watch: {
    gObj: {
      deep: true,
      handler() {
        // 拿到商品数量, 判断小于1, 强制修改成1
        if (this.gObj.goods_count < 1) {
          this.gObj.goods_count = 1;
        }
      }
    }
  }
};
</script>

<style lang="less" scoped>
.my-goods-item {
  display: flex;
  padding: 10px;
  border-bottom: 1px solid #ccc;
  .left {
    img {
      width: 120px;
      height: 120px;
      margin-right: 8px;
      border-radius: 10px;
    }
    .custom-control-label::before,
    .custom-control-label::after {
      top: 50px;
    }
  }
  .right {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    .top {
      font-size: 14px;
      font-weight: 700;
    }
    .bottom {
      display: flex;
      justify-content: space-between;
      padding: 5px 0;
      align-items: center;
      .price {
        color: red;
        font-weight: bold;
      }
    }
    .my-counter {
      display: flex;
      .inp {
        width: 45px;
        text-align: center;
        margin: 0 10px;
      }
      .btn,
      .inp {
        transform: scale(0.9);
      }
    }
  }
}
</style>
